package at.fhj.itm10.ticketmaster.beans;

import java.util.HashMap;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.ManagedBean;
import javax.annotation.PostConstruct;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ValueChangeEvent;

import at.fhj.itm10.ticketmaster.services.CartService;
import at.fhj.itm10.ticketmaster.services.ServiceFactory;

/**
 * @author Martin Kropf, Wolfgang Schrattner, Reinhard Rebernig, Gabi Grüner
 * @since Jänner 2013
 * 
 * CartBean for several operations in cart
 * 
 */

@ManagedBean
@SessionScoped
public class CartBean{
	
	private CartService cartservice;
	
	/**
	 *  initializing Bean and reseting cart
	 */
	public CartBean() {
		init();
		cartservice.setTotalTicketCount(0);
	}
	
	/**
	 * cartservice object for cart operations
	 */
	@PostConstruct
	public void init(){
		ServiceFactory servicefactory = new ServiceFactory();
		cartservice = servicefactory.getCartService();		
	}
	
	/**
	 * Setting ticket quantity depending on value of dropdown menu (param e)
	 * @param e
	 */
	public void valueChangeMethod(ValueChangeEvent e) {
		setTicketQuantity((String) e.getNewValue());
	}
	
	/**
	 * returns quantities for dropdownmenu
	 * @return
	 */
	public Map<String, Object> getQuantities(){	
		return cartservice.getQuantities();
	}

	/**
	 * trys to add tickets to cart and returns true/false if ticket(s) could be added to cart
	 * @param id
	 * @return
	 */
	public Boolean addToCart(String id) {
		return cartservice.addToCard(id);
	}
	
	/**
	 * returns boolean if adding tickets failed or were successful
	 * @return
	 */
	public Boolean getticketamountisFaild() {
		return cartservice.getticketamountisFaild();
	}
		
	/**
	 * gets quantity and price of specific ticket and adds sum to totalprice array
	 * @param singlePrice
	 * @param quantity
	 */
	public void addTicketPrice(double singlePrice, int quantity) {
		cartservice.addTicketPrice(singlePrice, quantity);
	}
	
	/**
	 * returns the total price by getting value through iterating totalprice array
	 * @return
	 */
	public String getTotalPrice() {
		return cartservice.getTotalPrice();
	}

	/**
	 * removes specific ticket from cart
	 * @param id
	 * @return
	 */
	public String removeFromCart(String id) {
		return cartservice.removeFromCart(id);
	}
	
	/**
	 * removes all items from cart
	 */
	public void emptyCart() {
		cartservice.emptyCart();
	}

	/**
	 * returns tickets and quantities as hashmap
	 * @return
	 */
	public HashMap<String, Integer> showTickets() {
		return cartservice.showTickets();
	}
	
	/**
	 * returns List which contains number of different tickets in cart
	 * @return
	 */
	public List<Entry<String, Integer>> getTicketsList() {
		return cartservice.getTicketsList();
	}

	/**
	 * returns total amount of all tickets in cart
	 * @return
	 */
	public int getTotalTicketCount() {
		return cartservice.getTotalTicketCount();
	}

	/**
	 * refreshes total amount of all tickets in cart
	 * @param totalTicketCount
	 */
	public void setTotalTicketCount(int totalTicketCount) {
		cartservice.setTotalTicketCount(totalTicketCount);
	}

	/**
	 * returns quantity of specific ticket
	 * @return
	 */
	public String getTicketQuantity() {
		return cartservice.getTicketQuantity();
	}

	/**
	 * sets ticket quantity depending on dropdown menu
	 * @param ticketQuantity
	 */
	public void setTicketQuantity(String ticketQuantity) {
		cartservice.setTicketQuantity(ticketQuantity);
	}
		
}
